package com.jhgsys.internal.sypq.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.common.utils.DateUtil;
import com.jhgsys.internal.sypq.entity.*;
import com.jhgsys.internal.sypq.mapper.TaskMapper;
import com.jhgsys.internal.sypq.service.*;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.jhgsys.internal.common.utils.FebsUtil.getCurrentUser;
import static com.jhgsys.internal.sypq.service.impl.ScheduleServiceImpl.mergeDateTime;

/**
 * 任务管理 Service实现
 *
 * @author ganjun
 * @date 2024-11-18 10:06:38
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProgressServiceImpl extends ServiceImpl<TaskMapper, Task> implements IProgressService {
     @Autowired
     IScheduleService scheduleService;

    @Autowired
    private ISypqProjectService projService;

    @Autowired
    private INoticeService noticeService;

    @Override
    public List<ProgressDto> findTaskPage(TaskDto taskDto,Boolean isAdmin,String userid) {
         List<TaskDto>  list= this.scheduleService.getAssembledTask(taskDto);
         //只有排期的项目才显示进度
         List<ProgressDto> progressDtoList=new ArrayList<>();
         for(TaskDto obj:list){
             ProgressDto progressDto=new ProgressDto();
             progressDto.setId(obj.getTaskCode());
             progressDto.setLevel(1);
             progressDto.setManagers(obj.getManagers());
             progressDto.setProjectCode(obj.getProjectCode());
             progressDto.setProjectName(obj.getProjectName());
             progressDto.setTaskCode(obj.getTaskCode());
             obj.setIsDrag(false);
             List taskList=new ArrayList();
             taskList.add(obj);
             progressDto.setTaskList(taskList);
             if(isAdmin||  StringUtils.isNotEmpty(obj.getManagerids()) && Arrays.asList(obj.getManagerids().split(",")).contains(userid)
                || userid.equals(obj.getRsDirector()) || userid.equals(obj.getAuditorid())) {
                 progressDtoList.add(progressDto);
             }
         }
         return  progressDtoList;
    }






   public  void  pauseTask(TaskDto taskDto){
        Task task=this.baseMapper.selectById(taskDto.getTaskCode());
        task.setPauseDate(taskDto.getPlanEnddate());
        task.setPauseTime(taskDto.getPlanEndtime());
        task.setStatus(7);
        task.setPauseReason(taskDto.getPauseReason());
        task.setActuallyEnddate(mergeDateTime(taskDto.getPauseDate(),taskDto.getPauseTime()));
         //更新计划完成，以更新进度条
       task.setPlanEnddate(taskDto.getPlanEnddate());
       task.setPlanEndtime(taskDto.getPlanEndtime());
        this.baseMapper.updateById(task);
       try{
           //待办通知
           String content = "("+task.getProjectCode()+"项目"+taskDto.getTaskCode()+")暂停，暂停时间为"+DateUtil.getDateFormat(taskDto.getPauseDate(), "yyyy-MM-dd HH:mm")+"，暂停原因（"+taskDto.getPauseReason()+"），请及时关注" ;
           Notice notice = new Notice();
           notice.setNoticeType(1);
           notice.setPendingType("项目任务暂停通知");
           notice.setNoticeTitle("项目任务暂停通知");
           notice.setNoticeContent(content);
           if(task != null){
               SypqProject proj = projService.findSypqProjectById(task.getProjectCode());
               if(proj != null){
                   notice.setReceiveUserId(proj.getAuditorid());
               }
           }
           notice.setUrl("");
           noticeService.createNotice(notice);
       }catch (Exception e){
           log.error("pauseTask: notice error:", e);
       }
    }

    public  void  finishTask(TaskDto taskDto){
        Task task=this.baseMapper.selectById(taskDto.getTaskCode());
        task.setPlanEnddate(taskDto.getPlanEnddate());
        task.setPlanEndtime(taskDto.getPlanEndtime());
        task.setActuallyEnddate(mergeDateTime(taskDto.getPlanEnddate(),taskDto.getPlanEndtime()));
        task.setStatus(6);
        this.baseMapper.updateById(task);

        try{
            //待办通知
            String content = "("+taskDto.getProjectName()+"项目"+taskDto.getTaskCode()+")提前完成，实际开始时间为"+ DateUtil.getDateFormat(task.getActuallyStartdate(), "yyyy-MM-dd HH:mm")+"，实际完成时间为"+task.getActuallyEnddate()+"，请及时关注" ;
            Notice notice = new Notice();
            notice.setNoticeType(1);
            notice.setPendingType("项目任务提前完成通知");
            notice.setNoticeTitle("项目任务提前完成通知");
            notice.setNoticeContent(content);
            if(task != null){
                SypqProject proj = projService.findSypqProjectById(task.getProjectCode());
                if(proj != null){
                    notice.setReceiveUserId(proj.getAuditorid());
                }
            }
            notice.setUrl("");
            noticeService.createNotice(notice);
        }catch (Exception e){
            log.error("finishTask: notice error:", e);
        }
    }

}
